home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / rwvector.lha / RWVector2.1 / rw / complex.h < prev    next >
C/C++ Source or Header  |  1989-08-18  |  3KB  |  136 lines

  1. #ifndef COMPLEXH
  2. #define COMPLEXH
  3.  
  4. /* complex.h --- Declarations for type complex, for use with the AT&T
  5.    "cfront" compiler.  Modified from the original code distributed with
  6.    the compiler.
  7.  
  8.    Modified by 
  9.  
  10.     Tom Keffer
  11.     School of Oceanography, WB-10
  12.     Univ. of Washington
  13.     Seattle, WA 98195
  14.     (206) 543-6455
  15.  
  16.     Internet: keffer@sperm.ocean.washington.edu
  17.     uucp:     uw-beaver!sperm.ocean.washington.edu!keffer
  18.     BITNET:   keffer%sperm.ocean.washington.edu@UWAVM
  19.     Telemail: T.KEFFER/OMNET     
  20.  
  21. Changes from standard AT&T release:
  22.     1) Addition of an unadorned constructor.
  23.     2) Removal of the default value for the real argument of
  24.     the existing constructor.
  25.     3) Most inline functions changed to inline within
  26.     declaration.
  27. */
  28.  
  29. #include <stream.h>
  30. #include <errno.h>
  31.  
  32. overload cos;
  33. overload cosh;
  34. overload exp;
  35. overload log;
  36. overload pow;
  37. overload sin;
  38. overload sinh;
  39. overload sqrt;
  40. overload abs;
  41.  
  42. #include <math.h>
  43. inline double abs(double d) { return fabs(d); }
  44.  
  45. class complex {
  46.     double    re, im;
  47. public:
  48.     /* These constructors changed around by tk */
  49.     complex()            {re=0; im=0;}
  50.     complex(double r)        {re=r; im=0; }
  51.     complex(double r, double i)    {re=r; im=i; }
  52.          
  53.     friend    double    real(const complex& a) {return a.re;}
  54.     friend    double    imag(const complex& a) {return a.im;}
  55.  
  56.     friend    double    abs(complex);
  57.     friend  double  norm(complex);
  58.     friend  double    arg(complex);
  59.     friend  complex conj(complex a){
  60.       return complex(a.re, -a.im);
  61.     }
  62.     friend  complex cos(complex);
  63.     friend  complex cosh(complex);
  64.     friend    complex exp(complex);
  65.     friend  complex log(complex);
  66.     friend  complex pow(double, complex);
  67.     friend    complex pow(complex, int);
  68.     friend    complex pow(complex, double);
  69.     friend    complex pow(complex, complex);
  70.     friend  complex    polar(double, double = 0);
  71.     friend  complex sin(complex);
  72.     friend  complex sinh(complex);
  73.     friend    complex sqrt(complex);
  74.  
  75.     friend    complex    operator+(complex a1, complex a2)
  76.       { return complex(a1.re+a2.re, a1.im+a2.im); }
  77.     friend    complex    operator-(complex a)
  78.       { return complex(-a.re, -a.im); }
  79.     friend    complex operator-(complex a1, complex a2)
  80.       { return complex(a1.re-a2.re, a1.im-a2.im); }
  81.     friend    complex operator*(complex, complex);
  82.     friend     complex operator/(complex, complex);
  83.     friend     int    operator==(complex a, complex b)
  84.       { return (a.re==b.re && a.im==b.im); }
  85.     friend     int    operator!=(complex a, complex b)
  86.       { return (a.re!=b.re || a.im!=b.im); }
  87.     
  88.         void operator+=(complex);
  89.     void operator-=(complex);
  90.     void operator*=(complex);
  91.     void operator/=(complex);
  92.  
  93. };
  94.  
  95. ostream& operator<<(ostream&, complex);
  96. istream& operator>>(istream&, complex&);
  97.  
  98. extern int errno;
  99.  
  100. inline void complex::operator+=(complex a)
  101. {
  102.     re += a.re;
  103.     im += a.im;
  104. }
  105.  
  106. inline void complex::operator-=(complex a)
  107. {
  108.     re -= a.re;
  109.     im -= a.im;
  110. }
  111.  
  112. static const complex complex_zero(0,0);
  113.  
  114. class c_exception
  115. {
  116.     int    type;
  117.     char    *name;
  118.     complex    arg1;
  119.     complex    arg2;
  120.     complex    retval;
  121. public:
  122.  
  123.     c_exception( char *n, complex a1, complex a2 = complex_zero )
  124.         { name = n; arg1 = a1; arg2 = a2; type = 0; retval = 0; }
  125.  
  126.     friend int complex_error( c_exception& );
  127.  
  128.     friend complex exp( complex );
  129.     friend complex sinh( complex );
  130.     friend complex cosh( complex );
  131.     friend complex log( complex );    
  132. };
  133.  
  134.  
  135. #endif
  136.